package net.xdclass.controller;


import net.xdclass.controller.request.LinkGroupAddRequest;
import net.xdclass.controller.request.LinkGroupUpdateRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.service.LinkGroupService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2021-12-09
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {

    @Autowired
    private LinkGroupService linkGroupService;

    @PostMapping("add")
    public JsonData add(@RequestBody LinkGroupAddRequest linkGroupAddRequest){
        int row = linkGroupService.add(linkGroupAddRequest);
        return row == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }

    @DeleteMapping("/del/{group_id}")
    public JsonData delete(@PathVariable("group_id") Long groupId) {
        int row = linkGroupService.del(groupId);
        return row == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }

    @GetMapping("/detail/{group_id}")
    public JsonData detail(@PathVariable("group_id") Long groupId){
        LinkGroupVO detail = linkGroupService.detail(groupId);

        return JsonData.buildSuccess(detail);

    }

    @GetMapping("list")
    public JsonData findUserAllLinkGroup(){
        List<LinkGroupVO> linkGroupVOS = linkGroupService.listAllGroup();
        return JsonData.buildSuccess(linkGroupVOS);
    }


    @PutMapping("update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest linkGroupUpdateRequest){
        int row = linkGroupService.updateById(linkGroupUpdateRequest);
        return row == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL) ;
    }

}

